package com.todo.strategy.offline.infrastructure.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.common.utils.MathUtils;
import com.todo.common.utils.StringUtils;
import com.todo.strategy.offline.infrastructure.entity.condition.PlanSetCondition;
import com.todo.strategy.offline.infrastructure.entity.po.PlanSetPO;
import com.todo.strategy.offline.infrastructure.mapper.PlanSetMapper;
import com.todo.strategy.offline.infrastructure.repository.PlanSetRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * (PlanSet)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 21:10:04
 */
@Slf4j
@Repository("planSetRepository")
public class PlanSetRepositoryImpl implements PlanSetRepository {
    @Resource
    private PlanSetMapper planSetMapper;

    @Override
    public Boolean removePlanSetPOById(Long id) {
        if (id == null || id <= 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }

        return planSetMapper.removePlanSetPOById(id) > 0;
    }

    @Override
    public PlanSetPO queryPlanSetPOById(Long id) {
        if (id == null || id <= 0) {
            return null;
        }
        return planSetMapper.queryPlanSetPOById(id);
    }

    @Override
    public Boolean insertPlanSetPO(PlanSetPO planSetPO) {
        if (planSetPO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(planSetPO)) {
            return false;
        }
        return planSetMapper.insertPlanSetPO(planSetPO) > 0;
    }


    @Override
    public Boolean updatePlanSetPOById(PlanSetPO planSetPO) {
        if (planSetPO == null || planSetPO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        if (!baseFieldCheck(planSetPO)) {
            return false;
        }
        return planSetMapper.updatePlanSetPOById(planSetPO) > 0;
    }

    @Override
    public PageResult<PlanSetPO> queryPlanSetPOByCondition(PlanSetCondition planSetCondition) {
        PageResult<PlanSetPO> pageResult = new PageResult<>();
        if (planSetCondition == null) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        pageResult.setPageNum(planSetCondition.getPageIndex());
        pageResult.setPageSize(planSetCondition.getPageSize());
        long count = queryCountByCondition(planSetCondition);
        if (count <= 0) {
            return pageResult;
        }
        pageResult.setPageCount((int) MathUtils.divide(count, planSetCondition.getPageSize()));
        pageResult.setTotal(count);
        List<PlanSetPO> sightPoiPOList = planSetMapper.queryPlanSetByCondition(planSetCondition);
        if (CollectionUtils.isEmpty(sightPoiPOList)) {
            pageResult.setHasNext(false);
            return pageResult;
        }
        pageResult.setResults(sightPoiPOList);
        pageResult.setCurrentIndex(sightPoiPOList.get(0).getId());
        pageResult.setHasNext(sightPoiPOList.size() >= planSetCondition.getPageSize());
        return pageResult;
    }

    @Override
    public Long queryCountByCondition(PlanSetCondition planSetCondition) {
        if (planSetCondition == null || planSetCondition.getPageSize() <= 0) {
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        return planSetMapper.queryCountByCondition(planSetCondition);
    }

    @Override
    public PlanSetPO queryPlanSetByUserAndTitle(Long userId, String title) {
        if (userId == null || userId <= 0 || StringUtils.isBlank(title)){
            return null;
        }
        return planSetMapper.queryPlanSetByUserAndTitle(userId, title);
    }

    @Override
    public List<PlanSetPO> queryPlanSetPOByUserId(Long userId) {
        if (userId == null || userId < 0){
            return Collections.emptyList();
        }

        return planSetMapper.queryPlanSetPOByUserId(userId);
    }

    @Override
    public Boolean removePlanSetByIdList(Long userId, List<String> planIdList) {
        if (userId == null || userId < 0){
            return false;
        }
        if (CollectionUtils.isEmpty(planIdList)){
            return true;
        }
        return planSetMapper.removePlanSetByIdList(userId, planIdList);
    }

    @Override
    public List<PlanSetPO> queryPlanSetListByTime(Long userId, LocalDateTime startTime, LocalDateTime endTime) {
        if (userId == null || userId < 0){
            return Collections.emptyList();
        }
        if (startTime == null || endTime == null || startTime.isAfter(endTime)){
            return Collections.emptyList();
        }
        return planSetMapper.queryPlanSetListByTime(userId, startTime, endTime);
    }

    @Override
    public List<PlanSetPO> queryPlanSetByIdList(List<Long> planIdList) {
        if (CollectionUtils.isEmpty(planIdList)){
            return Collections.emptyList();
        }
        return planSetMapper.queryPlanSetByIdList(planIdList);
    }

    private boolean baseFieldCheck(PlanSetPO planSetPO) {
        if (planSetPO == null) {
            return false;
        }
        if (planSetPO.getUserId() == null || planSetPO.getStartPlanTime() == null || planSetPO.getEndPlanTime() == null || planSetPO.getPlanCity() == null) {
            log.warn("PlanSetRepository#baseFieldCheck 核心字段校验失败，默认操作失败 planSetPO：【{}】", JSONObject.toJSONString(planSetPO));
            return false;
        }
        if (planSetPO.getPlanType() == null || planSetPO.getIsPrivate() == null) {
            log.warn("PlanSetRepository#baseFieldCheck 基础字段校验失败，默认操作失败 planSetPO：【{}】", JSONObject.toJSONString(planSetPO));
            return false;
        }
        return true;
    }
}
